function lengthen(scale, accuracy, size, style, unitstyle, boolIfReverse, boolIfReverseOffset, boolIfAddUnit) {
  var drawingScale = scale;
  var dataAccuracy = accuracy;
  var fontSize = size;
  var lineStyle = style;
  var unit = unitstyle;
  var ifReverse = boolIfReverse;
  var ifReverseOffset = boolIfReverseOffset;
  var ifAddUnit = boolIfAddUnit;
  // 转换单位
  // 转换成毫米
  function pointToMM(numIn) {
    return (numIn / 72) * 25.4;
  }
  // 转换成米（打印大小）
  function pointToM(numIn) {
    return (((numIn / 72) * 25.4) / 1000) * drawingScale;
  }
  // 毫米转换成点
  function MMTopoint(numIn) {
    return (numIn / 25.4) * 72;
  }
  // 米转换成点（打印大小）
  function MTopoint(numIn) {
    return (((numIn * 1000) / 25.4) * 72) / drawingScale;
  }
  // 格式化数字
  if (documents.length > 0) {
    var activeDocument = app.activeDocument;
    var elementsSelected = activeDocument.selection;
    if (elementsSelected.length > 0) {
      for (i = 0; i < elementsSelected.length; i++) {
        var targetItem = elementsSelected[i];

        // 创建文本
        var areaText = elementsSelected[i].parent.textFrames.add();
        areaText.contents = formatNum(pointToMM(targetItem.length * drawingScale), dataAccuracy);

        // 添加单位
        if (ifAddUnit) {
          var unitText;
          switch (unit) {
            case 0:
              unitText = "mm";
              break;
            case 1:
              unitText = "cm";
              break;
            case 2:
              unitText = "m";
              break;
          }
          areaText.contents += unitText;
        }

        // 设置文本字体
        var fontYaHei = -1;
        var tryCount = 10;

        for (n = 0; n < app.textFonts.length; n++) {
          if (app.textFonts[n].name == "MicrosoftYaHei") {
            fontYaHei = n;
          }
        }

        while (fontYaHei > 0 && areaText.textRange.characterAttributes.textFont.name != app.textFonts[fontYaHei].name && tryCount > 0) {
          areaText.textRange.characterAttributes.textFont = app.textFonts[fontYaHei];
          tryCount--;
        }

        // 设置字体大小
        var textSizeChanged = false;
        var originalTextSize = areaText.textRange.characterAttributes.size;
        var tryCount = 10;

        while (!textSizeChanged && tryCount > 0) {
          areaText.textRange.characterAttributes.size = MMTopoint(fontSize).toFixed(3);
          tryCount--;

          if (areaText.textRange.characterAttributes.size != originalTextSize) {
            textSizeChanged = true;
          }
        }

        // 旋转相关
        var whetherRotate = false;
        var tryCount = 10;

        if (areaText.tags.length <= 0 || areaText.tags[0].name != "BBAccumRotation") {
          BBAccumRotationTAG = areaText.tags.add();
          BBAccumRotationTAG.name = "BBAccumRotation";
          BBAccumRotationTAG.value = 0;

          if (targetItem.tags.length > 0 && targetItem.tags[0].name == "BBAccumRotation") {
            var rotation;
            // 使标注始终朝上
            // 需要考虑两套旋转坐标，还要考虑精度问题
            if (ifReverse) {
              areaText.tags[0].value = targetItem.tags[0].value;
              rotation = (180 * targetItem.tags[0].value) / Math.PI;
              if (rotation >= 90 && rotation <= 180) {
                areaText.tags[0].value += 180;
                rotation += 180;
              } else if (rotation <= -89 && rotation >= -181) {
                areaText.tags[0].value += 180;
                rotation += 180;
              } else if (rotation > 180 && rotation <= 271) {
                areaText.tags[0].value += 180;
                rotation += 180;
              }
            } else {
              areaText.tags[0].value = targetItem.tags[0].value;
              rotation = (180 * targetItem.tags[0].value) / Math.PI;
            }

            // 文本原始角度
            var originalRotation = ((180 * areaText.tags[0].value) / Math.PI).toFixed(3);
            while (!whetherRotate && tryCount > 0) {
              // 使文本角度与其对应的尺寸线角度相同
              areaText.rotate(rotation);
              tryCount--;

              if (((180 * areaText.tags[0].value) / Math.PI).toFixed(3) == originalRotation) {
                whetherRotate = true;
              }
            }

            // debug
            if (tryCount <= 0) {
              alert("Rotation failed: " + "tryCount <= 0");
            }
          }
        }

        // 设置文本坐标
        var whetherMove = false;
        var originalTop = areaText.top;
        var originalLeft = areaText.left;
        var tryCount = 10;
        
        while (!whetherMove && tryCount > 0) {
          areaText.top = targetItem.top - targetItem.height / 2 + areaText.height / 2;
          areaText.left = targetItem.left + targetItem.width / 2 - areaText.width / 2;
          tryCount--;

          if (areaText.top != originalTop || areaText.left != originalLeft) {
            whetherMove = true;
          }
        }

        // debug
        if (tryCount <= 0) {
          alert("Text Move failed: " + "tryCount <= 0");
        }

        // 文本偏移
        if (ifReverse) {
          var degree = (180 * areaText.tags[0].value) / Math.PI;
          if ((degree >= 89 && degree <= 91) || (degree <= -89 && degree >= -91) || (degree >= 269 && degree <= 271)) {
            if (ifReverseOffset) {
              areaText.left += MMTopoint(fontSize / 1.0);
            } else {
              areaText.left -= MMTopoint(fontSize / 1.6);
            }
          } else if ((degree >= -1 && degree <= 1) || (degree >= 179 && degree <= 181) || (degree <= -179 && degree >= -181)) {
            if (ifReverseOffset) {
              areaText.top -= MMTopoint(fontSize / 1.0);
            } else {
              areaText.top += MMTopoint(fontSize / 1.6);
            }
          } else if ((degree > 1 && degree < 89) || (degree < -91 && degree > -179) || (degree > 181 && degree < 269)) {
            areaText.top += fontSize * 1.4;
            areaText.left -= fontSize * 1.4;
          } else if ((degree > 91 && degree < 179) || (degree > -89 && degree < -1) || (degree > 271 && degree < 359)) {
            areaText.top += fontSize * 1.4;
            areaText.left += fontSize * 1.4;
          }
        } else {
          var degree = (180 * areaText.tags[0].value) / Math.PI;
          if (degree >= 89 && degree <= 91) {
            areaText.left -= MMTopoint(fontSize / 1.5);
          } else if ((degree <= -89 && degree >= -91) || (degree >= 269 && degree <= 271)) {
            areaText.left += MMTopoint(fontSize / 1.5);
          } else if (degree >= -1 && degree <= 1) {
            areaText.top += MMTopoint(fontSize / 1.5);
          } else if ((degree >= 179 && degree <= 181) || (degree <= -179 && degree >= -181)) {
            areaText.top -= MMTopoint(fontSize / 1.5);
          } else if (degree > 1 && degree < 89) {
            areaText.top += fontSize * 1.4;
            areaText.left -= fontSize * 1.4;
          } else if (degree > 91 && degree < 179) {
            areaText.top -= fontSize * 1.4;
            areaText.left -= fontSize * 1.4;
          } else if ((degree > -179 && degree < -91) || (degree > 181 && degree < 269)) {
            areaText.top -= fontSize * 1.4;
            areaText.left += fontSize * 1.4;
          } else if ((degree > -89 && degree < -1) || (degree > 271 && degree < 359)) {
            areaText.top += fontSize * 1.4;
            areaText.left += fontSize * 1.4;
          }
        }

        // 设置直线样式
        var rule = /^[0-9]+$/;
        if (rule.test(lineStyle) && lineStyle < activeDocument.graphicStyles.length) {
          activeDocument.graphicStyles[lineStyle].applyTo(targetItem);
        } else {
          alert("样式序号不合法!");
        }

        redraw();
        targetItem.selected = false;
      }
    } else {
      // alert("请先选中一个物体!");
    }
  } else {
    alert("请先打开一个文档!");
  }
}

function formatNum(numIn, accuracyIn) {
  var tempNum = numIn.toFixed(accuracyIn + 2).slice(0, -1);
  var tempNumString = "" + tempNum;
  var lastNum = parseInt(tempNumString.charAt(tempNumString.length - 1));
  if (accuracyIn == 0) {
    return (numIn = numIn.toFixed());
  }
  if (lastNum != 0) {
    var zeroString = "";
    var tempString = "";
    for (k = 1; k < accuracyIn; k++) {
      zeroString += "0";
    }
    tempString = "0." + zeroString + "1";
    // 此时area为字符串
    numIn = parseFloat(numIn) + parseFloat(tempString);
    numIn = numIn.toFixed(accuracyIn + 1).slice(0, -1);
  } else {
    numIn = numIn.toFixed(accuracyIn + 1).slice(0, -1);
  }
  return numIn;
}
